<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <el-tab-pane label="角色管理">
            <el-row>
              <el-button icon="el-icon-plus" type="primary" size="small" @click="showDialog = true">
                新增角色
              </el-button>
            </el-row>
            <!-- 表格 -->
            <el-table border="" :data="roleslist" style="margin-top:20px">
              <el-table-column align="center" type="index" label="序号" />
              <el-table-column align="center" label="角色名称" prop="name" />
              <el-table-column align="center" label="描述" prop="description" />
              <el-table-column align="center" label="操作">
                <!-- 作用域插槽 -->
                <template slot-scope="{ row }">
                  <el-button size="small" type="success">分配权限</el-button>
                  <el-button size="small" type="primary" @click="editDialog(row.id)">编辑</el-button>
                  <el-button size="small" type="danger" @click="delRolesInfo(row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <!-- 分页 -->
            <el-row type="flex" justify="center" align="middle" style="height:60px">
              <el-pagination
                :current-page="roleslist.page"
                :page-size="roleslist.pagesize"
                layout="prev, pager, next"
                :total="roleslist.total"
                @current-change="changePage"
              />
            </el-row>
          </el-tab-pane>
          <!-- 右侧 公司信息 -->
          <el-tab-pane label="公司信息" name="second">
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
            />
            <!-- 表单 -->
            <el-form ref="form" :model="formData" label-width="120px" style="margin-top:20px" disabled>
              <el-form-item label="公司名称" style="width:400px">
                <el-input v-model="formData.name" />
              </el-form-item>
              <el-form-item label="公司地址" style="width:400px">
                <el-input v-model="formData.companyAddress" />
              </el-form-item>
              <el-form-item label="邮箱" style="width:400px">
                <el-input v-model="formData.mailbox" />
              </el-form-item>
              <el-form-item label="备注" style="width:400px">
                <el-input v-model="formData.remarks" type="textarea" rows="3" />
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
    <!-- 编辑弹层 -->
    <el-dialog
      title="编辑弹层"
      :visible="showDialog"
      width="70%"
      @close="btnCancel"
    >
      <el-form ref="rolesForm" :model="rolesForm" :rules="rules" label-width="120px">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="rolesForm.name" />
        </el-form-item>
        <el-form-item label="角色描述">
          <el-input v-model="rolesForm.description" />
        </el-form-item>
      </el-form>
      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="6">
          <el-button size="small" @click="btnCancel">取 消</el-button>
          <el-button size="small" type="primary" @click="btnOk">确 定</el-button>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import { getRolesList, getCompanyInfo, delRolesInfo, updateRolesInfo, getRoleDetail, addRole } from '@/api/settings'
import { mapGetters } from 'vuex'
export default {
  data() {
    return {
      formData: {
        name: '',
        companyAddress: '',
        mailbox: '',
        remarks: ''
      },
      list: {
        page: '1',
        pagesize: '10',
        total: ''
      },
      roleslist: [],
      currentPage: 1,
      showDialog: false,
      rolesForm: {
        name: '',
        description: ''
      },
      rules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }]
      }
    }
  },
  computed: {
    ...mapGetters(['companyId'])
  },
  created() {
    this.getRolesList() // 获取角色列表
    this.getCompanyInfo()
  },
  methods: {
    // 获取角色列表
    async getRolesList() {
      const { rows, total } = await getRolesList(this.list)
      this.roleslist = rows
      this.roleslist.total = total
    },
    changePage(val) {
      console.log(`每页 ${val} 条`)
    },
    // 获取公司信息
    async getCompanyInfo() {
      this.formData = await getCompanyInfo(this.companyId)
    },
    // 删除角色列表
    async delRolesInfo(id) {
      // 提示信息
      try {
        this.$confirm('确定要删除此角色吗')
        await delRolesInfo(id) // 调用删除接口
        this.getRolesList() // 重新加载数据
        this.$message.success('删除成功')
      } catch (err) {
        console.log(err)
      }
    },
    async editDialog(id) {
      this.roleForm = await getRoleDetail(id)
      this.showDialog = true // 为了不出现闪烁的问题 先获取数据 再弹出层
    },
    async btnOk() {
      try {
        this.$refs.rolesForm.validate()
        // 只有校验通过的情况下 才会执行await的下方内容
        // roleForm这个对象有id就是编辑 没有id就是新增
        if (this.rolesForm.id) {
          await updateRolesInfo(this.rolesForm)
        } else {
          // 新增业务
          await addRole(this.rolesForm)
        }
        // 重新拉取数据
        this.getRolesList()
        this.$message.success('操作成功')
        this.showDialog = false
      } catch (err) {
        console.log(err)
      }
    },
    btnCancel() {
      this.rolesForm = {
        name: '',
        description: ''
      }
      // 移除校验
      this.$refs.rolesForm.resetFields()
      this.showDialog = false
    }
  }
}
</script>

<style>

</style>
